using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class InventoryController{

    private bool opened = false;
	private static InventoryController instance = null;
	public static InventoryController GetInstance(){
		if (instance == null)
			instance = new InventoryController();
		return instance;
	}
//Constructor	
	private InventoryController(){}
//Miembros	
	private Inventory activeInventory;
	private GameObject inventoryGUI;
	private GUIPanelInventory inventoryPanel;
//Funciones		
	public void SetInventoryGUI(GameObject go){
		this.inventoryGUI = go;
		this.inventoryPanel = 
			inventoryGUI.transform.GetComponentInChildren<GUIPanelInventory>();
	}
	public void ShowGUI(bool active){
		if (inventoryGUI.activeSelf == false)
			inventoryGUI.SetActive(true);
		if (active){
			ClearSlots();
			//this.inventoryGUI.SetActive(active);
			DrawItems(true);
			DrawSlots();
			EnableGUI(active);
			
		} else{
			DrawItems(false);
			//this.inventoryGUI.SetActive(active);
            MouseController.instance.drawCursor = true;
			EnableGUI(active);
		}
	}
	public void EnableGUI(bool active){
		//Los Slots
		for(int i=0; i< this.inventoryPanel.slots.Count; i++){
			this.inventoryPanel.slots[i].SetActive(active);	
		}
		//El fondo.
		this.inventoryPanel.transform.FindChild("Fondo").gameObject
		.SetActive(active);
		//El collider del panel.
		this.inventoryPanel.collider.enabled = active;
	}
	public void DrawItems(bool active){
		List<Item> items = activeInventory.GetItemsList();
		if (active){
			foreach (Item item in items){
				item.gameObject.SetActive(active);	
			}
		} else {
			foreach (Item item in items){
				item.gameObject.SetActive(active);	
			}
		}
	}
    public void OpenInventory()
    {
        opened = !opened;
        ShowGUI(opened);
    }
	public void DrawSlots(){inventoryPanel.DrawSlots(activeInventory);}
	public void ClearSlots(){inventoryPanel.ClearSlots();}
	public void SetActiveInventory(Inventory inv){
		this.activeInventory = inv;
		DrawSlots();
	}
    public bool IsOpened() { return opened; }
	public Inventory GetActiveInventory(){return this.activeInventory; }
	public GUIPanelInventory GetGUIPanel(){return this.inventoryPanel;}
	public Camera GetGUICamera(){return this.inventoryGUI.GetComponent<Camera>();}
}
